<template>
	<view>
		<!--基础信息-->
		<view class="btName">基础信息</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">
				<text>供应商名称</text>
				<text class="redXingh">*</text>
			</text>
			<u-input v-model="name" placeholder="请输入供应商名称" />
		</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">
				<text>联系人</text>
				<text class="redXingh">*</text>
			</text>
			<u-input v-model="linkman" placeholder="请输入供应商联系人" />
		</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">联系方式
				<text class="redXingh">*</text>
			</text>
			<u-input v-model="mobile" placeholder="请输入联系方式" />
		</view>
		<view class="flex-white-plr26 ptb20 bdb_f5" @click="chooseAddrFun">
			<text class="mr26">地址</text>
			<u-input v-model="address" placeholder="请输入地址" />
		</view>
		<view @click="pickerSelectFun('经济类型')" class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">经济类型
				<text class="redXingh">*</text>
			</text>
			<view :class="ecotype ? '' : 'cBlack'">
				{{ecotype ? ecotype : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
		<view @click="pickerSelectFun('等级')" class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">等级</text>
			<view :class="level ? '' : 'cBlack'">
				{{level ? level : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
		<view @click="pickerSelectFun('行业')" class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">行业</text>
			<view :class="industry ? '' : 'cBlack'">
				{{industry ? industry : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
		
		<view class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">受理部门</text>
			<view class="ownerdept">
				<uni-data-picker :localdata="dept_list_items" popup-title="请选部门" @change="onchange" @nodeclick="onnodeclick">
				</uni-data-picker>
			</view>
		</view>
		
		<u-picker :params="pickerParams" :safe-area-inset-bottom="true" end-year="2100" v-model="pickerShow"
			@confirm="pickerConfirmFun" :mode="pickerMode"></u-picker>
			
		<u-select v-model="selectShow" :list="selectList" @confirm="selectConfirmFun"></u-select>
		<!--扩展信息-->
		<view class="btName">扩展信息</view>
		<view class="flex-white-plr26 ptb20 mt32">
			<u-input v-model="remark" placeholder="备注" maxlength="500" placeholderStyle="color: #303133;" type="textarea" />
		</view>
		<!--提交按钮-->
		<view class="submitView">
			<u-button type="primary" class="submitBtn" :ripple="true" ripple-bg-color="#909399" @click="submitBtn">
				{{pageType =='' ? '提交' : '保存'}}</u-button>
		</view>
	</view>
</template>

<script>
	let that = '';
	import {
		apiSupSupplier
	} from '@/api/crm.js'
	import {
		apiSysDept
	} from '@/api/admin.js'
	export default {
		data() {
			return {
				name: '',
				mobile: '',
				address: '',
				linkman: '',
				ecotype: '',
				level: '',
				industry: '',
				remark: '',
				owner_dept_id: '',
				owner_user_id: uni.$userInfo.id,
				pickerParams: {},
				pickerShow: false,
				pickerMode: 'time',
				selectShow: false,
				selectList: [],
				selectType: '',
				dept_list_items: [],
				supDict: '', //供应商字典
				pageType: '',
				xgInfoObj: {},
				isAdd: 0,
				fkId: ''
			}
		},
		onLoad(e) {
			that = this;
			if (e.type == 'update') {
				that.setDataFun();
				that.pageType = e.type;
				uni.setNavigationBarTitle({
					title: '修改供应商信息'
				})
			}
			
			//载入字典
			// 简化字典处理，直接使用预定义选项
			that.supDict = {
				ecotype: [
					{label: '一级供应商', value: '1'},
					{label: '二级供应商', value: '2'}
				],
				level: [
					{label: 'A级', value: '1'},
					{label: 'B级', value: '2'},
					{label: 'C级', value: '3'}
				],
				industry: [
					{label: '制造业', value: '1'},
					{label: '服务业', value: '2'},
					{label: '贸易业', value: '3'}
				]
			};

			
			that.getSysDept();

			if (e.isAdd) {
				that.isAdd = e.isAdd;
				if (uni.$addInfo) {
					that.linkman = uni.$addInfo.linkman;
					that.mobile = uni.$addInfo.mobile;
					that.name = uni.$addInfo.name;
					that.address = uni.$addInfo.address || '';
					if (uni.$addInfo.fkId) {
						that.fkId = uni.$addInfo.fkId;
					}
				}
			}
		},
		methods: {
			onchange(e) {
				const value = e.detail.value
			},
			onnodeclick(node) {
				that.owner_dept_id=node.value
			},
			//下拉选择框架
			pickerSelectFun: function(str) {
				if (str == '经济类型') {
					that.selectList = that.supDict.ecotype || [];
				} else if (str == '等级') {
					that.selectList = that.supDict.level || [];
				} else if (str == '行业') {
					that.selectList = that.supDict.industry || [];
				}
				that.selectShow = true;
				that.selectType = str;
			},
			selectConfirmFun: function(e) {
				let str = e[0].label;
				if (that.selectType == '经济类型') {
					that.ecotype = str;
				} else if (that.selectType == '等级') {
					that.level = str;
				} else if (that.selectType == '行业') {
					that.industry = str;
				}
			},
			// 选择定位地点
			chooseAddrFun: function() {
				uni.chooseLocation({
					success: function(res) {
						that.address = res.address;
					}
				});
			},
			// 提交按钮
			submitBtn: function() {
				if (that.name == '') {
					uni.showModal({
						title: '提示',
						content: '供应商名称不能为空，请输入！',
						showCancel: false
					})
					return
				}
				if (that.linkman == '') {
					uni.showModal({
						title: '提示',
						content: '联系人不能为空，请输入！',
						showCancel: false
					})
					return
				}
				if (that.mobile == '') {
					uni.showModal({
						title: '提示',
						content: '联系方式不能为空，请输入！',
						showCancel: false
					})
					return
				}
				//请求参数
				let reqObj = {
					req: {
						name: that.name,
						mobile: that.mobile,
						address: that.address,
						linkman: that.linkman,
						ecotype: that.ecotype,
						level: that.level,
						industry: that.industry,
						remark: that.remark,
						owner_user_id: that.owner_user_id,
						create_user_id: that.owner_user_id
					}
				}
				let action = 'add';
				if (that.pageType == 'update') {
					action = 'update'
					reqObj.req.id = that.xgInfoObj.id;
					uni.showLoading({
						title: '修改中...'
					})
				} else {
					reqObj.req.create_user_id = uni.$userInfo.id;
					reqObj.req.owner_user_id = uni.$userInfo.id;
					reqObj.req.openstatus = 1; //默认为私有
					if (that.fkId) {
						reqObj.req.fkId = that.fkId;
					}
					uni.showLoading({
						title: '提交中...'
					})
				}
				let reqData = {
					action: action,
					params: reqObj
				}
				apiSupSupplier(reqData)
					.then(res => {
						if (action == 'update') {
							uni.showToast({
								title: '供应商修改成功',
								icon: 'none',
								duration: 888,
								mask: true
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 888)
						} else {
							uni.showToast({
								title: '供应商新增成功',
								icon: 'none',
								duration: 1000,
								mask: true
							})
							let addObj = reqObj.req;
							if (that.isAdd) {
								uni.$emit('addItemInListFun', addObj)
							}
							setTimeout(() => {
								addObj.create_user_name = uni.$userInfo.nickname;
								addObj.owner_user_name = uni.$userInfo.nickname;
								uni.$supplierInfo = addObj;
								uni.redirectTo({
									url: './show'
								})
							}, 1000)
						}
					})
			},
			// 初始化修改数据
			setDataFun: function() {
				that.xgInfoObj = uni.$supplierInfo;
				let obj = uni.$supplierInfo;
				that.name = obj.name;
				that.mobile = obj.mobile;
				that.address = obj.address;
				that.linkman = obj.linkman;
				that.ecotype = obj.ecotype;
				that.level = obj.level;
				that.industry = obj.industry;
				that.remark = obj.remark;
			},
			// 获得部门数据
			getSysDept: function() {
				let that = this;
				let reqData = {
					action: 'get_list_tree',
					params: {
			
					}
				}
				apiSysDept(reqData)
					.then(res => {
						that.dept_list_items = res.data;
					});
			},
		}
	}
</script>

<style>
	page {
		background-color: #F8F8F8;
		padding-bottom: 260rpx;
	}

	/deep/ .input-value-border{border:solid 1px #ffffff !important;}
</style>